package com.example.tse.rbhttpservice.service.goods;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsFavoriteRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsFavoriteResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetList;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetListRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetListResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetPriceRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetPriceResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarInfoRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarInfoResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarList;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarListRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarListResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatInfoRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatInfoResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatListList;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatListRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatListResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;





/**
 *
 * @author wliu
 *    edi  tse
 */
public class GoodsServiceImpl implements GoodsService {
	private Context context;
	private static Gson gson = new Gson();


	public GoodsServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 收藏或取消收藏商品，若传递状态则根据值确定收藏或取消
	 * @param request
	 * @param listener
	 * @return GoodsFavoriteResponse
	 */

	    public void favorite(GoodsFavoriteRequest request, final RemoteServiceListenerT< GoodsFavoriteResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("goodsId",request.getGoodsId());
            params.put("status",request.getStatus());

        final String url = "goods/favorite";


            new HttpService().post(context, url, params, GoodsFavoriteResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GoodsFavoriteResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GoodsFavoriteResponse() : (GoodsFavoriteResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GoodsFavoriteResponse() : (GoodsFavoriteResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取虚拟分类一级详情
	 * @param request
	 * @param listener
	 * @return GoodsGetVirtualCatInfoResponse
	 */

	    public void getVirtualCatInfo(GoodsGetVirtualCatInfoRequest request, final RemoteServiceListenerT< GoodsGetVirtualCatInfoResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("virtualCatId",request.getVirtualCatId());

        final String url = "goods/getVirtualCatInfo";


            new HttpService().get(context, url, params, GoodsGetVirtualCatInfoResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GoodsGetVirtualCatInfoResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GoodsGetVirtualCatInfoResponse() : (GoodsGetVirtualCatInfoResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GoodsGetVirtualCatInfoResponse() : (GoodsGetVirtualCatInfoResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取不同类型的商品详情（类型有普通商品、秒杀商品、新品特卖、即将开售］），只包含商品的基本信息（价格、库存、限购等除外），秒杀商品需要秒杀状态（需要根据状态显示不同按钮）或倒计时，特卖商品中商品价格为特卖专门设置的价格，即将开售商品显示开售提醒按钮，分享的数据是商品第一张图＋商品名称＋固定描述文字＋链接
	 * @param request
	 * @param listener
	 * @return GoodsGetResponse
	 */

	    public void get(GoodsGetRequest request, final RemoteServiceListenerT< GoodsGetResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("productId",request.getProductId());
            params.put("type",request.getType());
            params.put("relativeId",request.getRelativeId());
            params.put("deviceId",request.getDeviceId());

        final String url = "goods/get";


            new HttpService().get(context, url, params, GoodsGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GoodsGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GoodsGetResponse() : (GoodsGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GoodsGetResponse() : (GoodsGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取独立专场详情
	 * @param request
	 * @param listener
	 * @return GoodsGetSimilarInfoResponse
	 */

	    public void getSimilarInfo(GoodsGetSimilarInfoRequest request, final RemoteServiceListenerT< GoodsGetSimilarInfoResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("similarId",request.getSimilarId());

        final String url = "goods/getSimilarInfo";


            new HttpService().get(context, url, params, GoodsGetSimilarInfoResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GoodsGetSimilarInfoResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GoodsGetSimilarInfoResponse() : (GoodsGetSimilarInfoResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GoodsGetSimilarInfoResponse() : (GoodsGetSimilarInfoResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取独立专场列表
	 * @param request
	 * @param listener
	 * @return GoodsGetSimilarListResponse
	 */

	    public void getSimilarList(GoodsGetSimilarListRequest request, final RemoteServiceListenerT< GoodsGetSimilarListResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "goods/getSimilarList";



        /**
        goods
        getSimilarList
        GetSimilarList
        获取独立专场列表
        GET
        Goods
        GoodsGetSimilarList
        goods/getSimilarList
        array
        */

           final Type type = new TypeToken<List<GoodsGetSimilarList>>() {
        }.getType();



            new HttpService().getList(context, url, params, GoodsGetSimilarListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<GoodsGetSimilarList> items = gson.fromJson(rudderResponse.getBody(), type);
                 GoodsGetSimilarListResponse responce = new GoodsGetSimilarListResponse();
                 responce.setGetSimilarList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new GoodsGetSimilarListResponse());
                 } else {
                     List<GoodsGetSimilarList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GoodsGetSimilarListResponse responce = new GoodsGetSimilarListResponse();
                     responce.setGetSimilarList(items);
                     listener.okData(rudderResponse == null ? new GoodsGetSimilarListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new GoodsGetSimilarListResponse(), fromWhere);
                 } else {
                     List<GoodsGetSimilarList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GoodsGetSimilarListResponse responce = new GoodsGetSimilarListResponse();
                     responce.setGetSimilarList(items);
                     listener.okData(rudderResponse == null ? new GoodsGetSimilarListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }






	/**
	 * 获取商品价格等信息（含价格、库存、限购数量、货品ID），商品信息对应的商品编号每个货品都需要改变的
	 * @param request
	 * @param listener
	 * @return GoodsGetPriceResponse
	 */

	    public void getPrice(GoodsGetPriceRequest request, final RemoteServiceListenerT< GoodsGetPriceResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("productId",request.getProductId());
            params.put("secondId",request.getSecondId());

        final String url = "goods/getPrice";


            new HttpService().get(context, url, params, GoodsGetPriceResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GoodsGetPriceResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GoodsGetPriceResponse() : (GoodsGetPriceResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GoodsGetPriceResponse() : (GoodsGetPriceResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取虚拟分类一级列表
	 * @param request
	 * @param listener
	 * @return GoodsGetVirtualCatListResponse
	 */

	    public void getVirtualCatList(GoodsGetVirtualCatListRequest request, final RemoteServiceListenerT< GoodsGetVirtualCatListResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "goods/getVirtualCatList";



        /**
        goods
        getVirtualCatList
        GetVirtualCatList
        获取虚拟分类一级列表
        GET
        Goods
        GoodsGetVirtualCatList
        goods/getVirtualCatList
        object
        */

           final Type type = new TypeToken<List<GoodsGetVirtualCatListList>>() {
        }.getType();



            new HttpService().getList(context, url, params, GoodsGetVirtualCatListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<GoodsGetVirtualCatListList> items = gson.fromJson(rudderResponse.getBody(), type);
                 GoodsGetVirtualCatListResponse responce = new GoodsGetVirtualCatListResponse();
                 responce.setList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new GoodsGetVirtualCatListResponse());
                 } else {
                     List<GoodsGetVirtualCatListList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GoodsGetVirtualCatListResponse responce = new GoodsGetVirtualCatListResponse();
                     responce.setList(items);
                     listener.okData(rudderResponse == null ? new GoodsGetVirtualCatListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new GoodsGetVirtualCatListResponse(), fromWhere);
                 } else {
                     List<GoodsGetVirtualCatListList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GoodsGetVirtualCatListResponse responce = new GoodsGetVirtualCatListResponse();
                     responce.setList(items);
                     listener.okData(rudderResponse == null ? new GoodsGetVirtualCatListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }



	/**
	 * 获取商品的列表，列表形式有普通商品列表、专场列表，默认排序最新倒叙（p_order最优先排序），特殊appmarketable字段标示当前商品是否可以在app中显示
	 * @param request
	 * @param listener
	 * @return GoodsGetListResponse
	 */

	    public void getList(GoodsGetListRequest request, final RemoteServiceListenerT< GoodsGetListResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("type",request.getType());
            params.put("brandId",request.getBrandId());
            params.put("lastest",request.getLastest());
            params.put("hotest",request.getHotest());
            params.put("sale",request.getSale());
            params.put("price",request.getPrice());
            params.put("catId",request.getCatId());

        final String url = "goods/getList";



        /**
        goods
        getList
        GetList
        获取商品的列表，列表形式有普通商品列表、专场列表，默认排序最新倒叙（p_order最优先排序），特殊appmarketable字段标示当前商品是否可以在app中显示
        GET
        Goods
        GoodsGetList
        goods/getList
        array
        */

           final Type type = new TypeToken<List<GoodsGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, GoodsGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<GoodsGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 GoodsGetListResponse responce = new GoodsGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new GoodsGetListResponse());
                 } else {
                     List<GoodsGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GoodsGetListResponse responce = new GoodsGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new GoodsGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new GoodsGetListResponse(), fromWhere);
                 } else {
                     List<GoodsGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GoodsGetListResponse responce = new GoodsGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new GoodsGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }






}